import java.io.IOException;

// -------------------------------------------------------------------------
/**
 *  This class represents a single command taken from the command parser.
 *  Each command has an identifier, perhaps a printype if it is a print command.
 *  Perhaps a sequence if it is an insert, remove, or search command, hence the
 *  two constructors.
 *
 *  @author Erik Rodriguez and Cory Chase.
 *  @version Mar 21, 2013
 */
public class Command {
	private int commandIdentifier; //Which command is being represented
	private String sequence;     //Sequence parameter if applicable
	private String ID;         //The sequence ID in string form
	private FileManager fMGR;  //THe file manager to write to the file
	private HashTable hashTable; //The hash table

	// ----------------------------------------------------------
	/**
	 * Create a new Command object. Pass in the tree to perform commands on
	 * and set the command identifier.
	 * @param cmd the identifier
	 * @param MGR the memory manager
	 * @param hashTable the hash table
	 */
	public Command(int cmd, FileManager MGR, HashTable hashTable){
		this.commandIdentifier = cmd;
		this.fMGR = MGR;
		this.hashTable = hashTable;

	}

	// ----------------------------------------------------------
	/**
	 * Create a new Command object. For a command that has a sequence as a
	 * parameter.
	 * @param cmd the command identifier
	 * @param seq the sequence parameter
	 * @param seqID the sequence ID
	 * @param MGR the memory manager
	 * @param hashTable the hash table
	 */
	public Command(int cmd, String seq,String seqID, FileManager MGR, HashTable hashTable){
		this.commandIdentifier = cmd;
		this.sequence = seq;
		this.ID = seqID;
		this.fMGR = MGR;
		this.hashTable = hashTable;
	}

	// ----------------------------------------------------------
	/**
	 * Create a new Command object without a sequence needed. For remove.
	 * @param cmd
	 * @param seqID
	 * @param MGR
	 * @param hashTable
	 */
	public Command(int cmd,String seqID, FileManager MGR, HashTable hashTable){
		this.commandIdentifier = cmd;
		this.ID = seqID;
		this.fMGR = MGR;
		this.hashTable = hashTable;
	}

	// ----------------------------------------------------------
	/**
	 * Execute the command held in this class, based off the command identifier.
	 * @throws IOException
	 */
	public void execute() throws IOException{
		switch(commandIdentifier){
		case Globals.INSERT_COMMAND:
			MemBlock[] memHandles = fMGR.insertSequence(sequence, ID);
			try{
				hashTable.insert(ID, memHandles[1], memHandles[0]);
			}
			catch(IllegalStateException ise){
				fMGR.removeSequence(memHandles[0], memHandles[1], true);
				System.out.println();
			}
			break;
		case Globals.PRINT_COMMAND:
			System.out.println("SequenceIDs: ");
			hashTable.print();
			fMGR.printFreeSpace();
			System.out.println();
			break;
		case Globals.REMOVE_COMMAND:
			hashTable.remove(ID);
			System.out.println();
			break;
		case Globals.SEARCH_COMMAND:
			hashTable.search(ID,true);
			System.out.println();
			break;
		}
	}
}
